using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using JigLibX.Physics;
using Microsoft.Xna.Framework.Input;
using sqengine.Components;
using sqengine.Engine.Common;

namespace sqengine
{
    public class ContentLoadingDrawer : Microsoft.Xna.Framework.DrawableGameComponent
    {
        private const int progress_step = 100;
        SpriteBatch spriteBatch;

        SpriteFont spriteFont;

        string _content_text;

        bool enabled = false;
        string _loading_font;
        string _back_image;
        Color _text_color;
        Texture2D _back_image_texture;

        AdvancedContent _adcontent;
        Game _game;
        private double _progresstm = progress_step;
        private int _prg_orientation = 1;
        private int _prg_index = 2;
        private string _prg_string = "[                               ]";
        private string _progress = "###";
        int rect_w = 510;
        int rect_h = 100;

        int bbWidth, bbHeight;

        public ContentLoadingDrawer(Game game, AdvancedContent adcontent, string content_text, Color content_color, string loading_font, string back_image)
            : base(game)
        {
            this._adcontent = adcontent;

            this._content_text = content_text;
            this._loading_font = loading_font;
            this._back_image = back_image;
            this._text_color = content_color;
            this._game = game;
            this.DrawOrder = 1;

        }

        private void GraphicsDevice_DeviceReset(object sender, EventArgs e)
        {
            bbWidth = GraphicsDevice.PresentationParameters.BackBufferWidth;
            bbHeight = GraphicsDevice.PresentationParameters.BackBufferHeight;
        }

        protected override void LoadContent()
        {
            this._game.GraphicsDevice.DeviceReset += new EventHandler(GraphicsDevice_DeviceReset);

            spriteBatch = new SpriteBatch(_game.GraphicsDevice);
            spriteFont = _adcontent.LoadSpriteFont(_loading_font);
            if (_back_image != "")
                _back_image_texture = _adcontent.LoadTexture2D(_back_image);


            base.LoadContent();
        }

        protected override void UnloadContent()
        {
            base.UnloadContent();
        }


        public override void Update(GameTime gameTime)
        {
            GraphicsDevice_DeviceReset(this, new EventArgs());

            _progresstm -= gameTime.ElapsedGameTime.TotalMilliseconds;

            if (_progresstm <= 0)
            {
                // [          ->...            ]
                _progresstm = progress_step;
                if (_prg_index + _progress.Length == _prg_string.Length + 1)
                {
                    _prg_orientation = _prg_orientation * -1;
                }
                else
                    if (_prg_index == 1)
                    {
                        _prg_orientation = _prg_orientation * -1;
                    }
                _prg_index = _prg_index + _prg_orientation;

            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            if (spriteBatch != null)
            {
                if (enabled)
                {
                    spriteBatch.Begin();
                    string progress_str = _prg_string.Insert(_prg_index, _progress);




                    if (_back_image_texture != null)
                    {
                        spriteBatch.Draw(_back_image_texture, new Rectangle(10, 10, 10, 10), Color.TransparentBlack);
                    }
                    else
                    {
                        spriteBatch.Draw(_adcontent.LoadTexture2D("Textures/UI/blank"), new Rectangle(0, 0, bbWidth, bbHeight), Color.Black);
                    }

                    spriteBatch.Draw(_adcontent.LoadTexture2D("Textures/UI/blank"), new Rectangle(_game.GraphicsDevice.Viewport.Width / 2 - rect_w / 2, _game.GraphicsDevice.Viewport.Height / 2 - rect_h / 2, rect_w, rect_h), Color.White);
                    spriteBatch.Draw(_adcontent.LoadTexture2D("Textures/UI/blank"), new Rectangle(_game.GraphicsDevice.Viewport.Width / 2 - rect_w / 2 + 1, _game.GraphicsDevice.Viewport.Height / 2 - rect_h / 2 + 1, rect_w - 2, rect_h - 2), Color.Black);

                    

                    spriteBatch.DrawString(spriteFont, progress_str, new Vector2(_game.GraphicsDevice.Viewport.Width / 2 - spriteFont.MeasureString(progress_str).X / 2, _game.GraphicsDevice.Viewport.Height / 2 - spriteFont.MeasureString(progress_str).Y / 2 - 20), _text_color);
                    if (_content_text != null)
                        spriteBatch.DrawString(spriteFont, _content_text, new Vector2(_game.GraphicsDevice.Viewport.Width / 2 - spriteFont.MeasureString(_content_text).X / 2, _game.GraphicsDevice.Viewport.Height / 2 - spriteFont.MeasureString(_content_text).Y / 2 + 15), _text_color);
                    spriteBatch.End();
                }


            }

            ((sqengine)this.Game).RestoreRenderState();

        }

        public void Start()
        {
            enabled = true;
        }

        public void Stop()
        {
            enabled = false;

        }
        public void UpdateLoadingText(string loadingtext, Color loadingcolor)
        {
            _content_text = loadingtext;
            _text_color = loadingcolor;
        }
    }
}
